# 项目：py-advance
# 文件：01增删改
# 作者：Frank Gong
# 日期：2025/4/10 12:03
# coding:utf-8

import pymysql

# 创建连接
connection = pymysql.connect(host='127.0.0.1',port=3306,
                             user='root',password='12345678',
                             db='hotel',charset='utf8')
# 创建游标
cursor = connection.cursor()

# 执行SQL
# cursor.execute("insert into emp(nickname,phone,idcard,sex,age,job) "
#                "values('蔡文姬','1103310231','782919222',1,30,'保洁')")
# 最后插入的一条数据ID
# new_id = cursor.lastrowid
# print(new_id)


# 执行SQL，并返回受影响行数
# query = cursor.execute("update emp set age = 16 where id = %s", (1))
# print(query)


# 执行SQL，并返回受影响行数,执行多次
query = cursor.executemany("insert into emp(nickname,phone,idcard,sex,age,job)"
                           "values(%s,%s,%s,%s,%s,%s)",
                           [('虞姬','210','728919223',1,19,'财务'),
                            ('霸王','33012','72891922213',0,43,'总经理')])
# 提交
connection.commit()
# 关闭游标和连接
cursor.close()
connection.close()